import React, { useState, useEffect } from 'react'
import axios from "../../../../model/User"
import { Table, Tag, Space, Button } from 'antd';
import Item from 'antd/lib/list/Item';
export default function UserCoupons() {
    let [datas, setdatas] = useState<any>([])
    let del = (id: any) => {
        axios.delusercoupons(id).then(() => {
            let id = window.location.hash.substring(25);
            axios.usercoupons({ uid: id }).then((res) => {
                // console.log(res.data);
                let arrs = res.data;
                let len = res.data.length;
                let arr: any = []
                for (let i = 0; i < len; i++) {
                    let obj = {
                        id: arrs[i].id,
                        time: arrs[i].time,
                        name: arrs[i].name,
                        denomination: arrs[i].denomination,
                        channel: arrs[i].channel,
                        state: arrs[i].state,
                        note: arrs[i].note,
                        del: <Button onClick={() => {
                            console.log(arrs[i].id)
                            del(arrs[i].id);
                        }} >删除</Button>
                    }
                    arr.push(obj)
                }
                setdatas(arr)
            })
        })



    }
    useEffect(() => {
        let id = window.location.hash.substring(25);
        axios.usercoupons({ uid: id }).then((res) => {
            // console.log(res.data);
            let arrs = res.data;
            let len = res.data.length;
            let arr: any = []
            for (let i = 0; i < len; i++) {
                let obj = {
                    id: arrs[i].id,
                    time: arrs[i].time,
                    name: arrs[i].name,
                    denomination: arrs[i].denomination,
                    channel: arrs[i].channel,
                    state: arrs[i].state,
                    note: arrs[i].note,
                    del: <Button onClick={() => {
                        console.log(arrs[i].id)
                        del(arrs[i].id);
                    }} >删除</Button>
                }
                arr.push(obj)
            }
            setdatas(arr)
        })
    }, [])

    const columns = [
        {
            title: '操作时间',
            dataIndex: 'time',
            key: 'time',
        },
        {
            title: '优惠券名称',
            dataIndex: 'name',
            key: 'name',
        },
        {
            title: '面值',
            dataIndex: 'denomination',
            key: 'denomination',
        },
        {
            title: '获取渠道',
            dataIndex: 'channel',
            key: 'channel',
        },
        {
            title: "使用状态",
            dataIndex: 'state',
            key: 'state',
        },
        {
            title: "操作备注",
            dataIndex: 'note',
            key: 'note',
        },
        {
            title: "操作",
            dataIndex: 'del',
            key: 'del',
        }

    ];

    const data = datas;
    return (
        <div>
            <Table columns={columns} dataSource={data} pagination={{ pageSize: 4 }}  rowKey="id"/>
        </div>
    )
}
